package com.tjetc.view.manage;

import com.tjetc.domain.Parent;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ParentService;
import com.tjetc.vo.Result;

import java.util.Scanner;

public class ParentsView {
    private static Scanner scanner=new Scanner(System.in);
    private ParentService parentsService= ClientProxy.getProxyInstance(ParentService.class);
    public void start() {
        while (true) {
            try {
                System.out.println("1.为新生儿添加家属");
                System.out.println("2.查询家属的信息");
                System.out.println("0.返回上一级");
                int i= scanner.nextInt();
                switch (i){
                    case 1:
                        addParent();
                        break;
                    case 2:
                        getParentById();
                        break;
                    case 0:
                        return;
                }
            } catch (Exception e) {
                System.out.println("输入错误,请重新输入");
                scanner.nextLine();
            }
        }
    }

    private void addParent() {
        System.out.println("请输入家属的姓名");
        String username = scanner.next();
        System.out.println("请输入家属的密码");
        String password = scanner.next();
        System.out.println("请输入家属的性别");
        String gender = scanner.next();
        System.out.println("请输入家属的电话");
        String phone;
        while (true) {
            phone = scanner.next();
            if(phone.matches("1[35789]\\d{9}")){
                break;
            }else
                System.out.println("手机号格式错误,请重新输入");
        }
        System.out.println("请输入家属的住址");
        String address = scanner.next();
        Parent parent = new Parent();
        parent.setUsername(username);
        parent.setPassword(password);
        parent.setGender(gender);
        parent.setPhone(phone);
        parent.setAddress(address);
        Result result = parentsService.add(parent);
        System.out.println(result.getMsg());

    }
    private void updateParent() {
        System.out.println("请输入家属的id");
        int id = scanner.nextInt();
        System.out.println("请输入你想要修改的家属姓名");
        String name = scanner.next();
        System.out.println("请输入你想要修改的家属的密码");
        String password = scanner.next();
        System.out.println("请输入你想要修改的家属的性别");
        String gender = scanner.next();
        System.out.println("请输入你想要修改的家属的电话");
        String phone = scanner.next();
        System.out.println("请输入你想要修改的家属的住址");
        String address = scanner.next();;
        Parent parent = new Parent();
        parent.setId(id);
        parent.setUsername(name);
        parent.setPassword(password);
        parent.setGender(gender);
        parent.setPhone(phone);
        parent.setAddress(address);
        Result result = parentsService.update(parent);
        System.out.println(result.getMsg());
        if (result.getCode()==200){
            System.out.println("修改成功");
        }else System.out.println("修改失败");
    }
    public void getParentById() {
        System.out.println("请输入家属的id");
        int id=  scanner.nextInt();
        Parent parentById = parentsService.getParentById(id);
        System.out.println(parentById);
    }
}
